<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <script type="text/javascript" src="../../layuiadmin/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../layuiadmin/layui/xadmin.js"></script>
    <script type="text/javascript" src="../../layuiadmin/js/system/jquery.min.js"></script>
    <script src="../../layuiadmin/js/system/jquery.cookie.js"></script>
    <script>
        $(function () {
            var tableuserId=$.cookie("tableUserId");
            var tableuserName;
            var role=$("#roleName");
            var  roleid;

            $.post("http://localhost:8080/user/listRole",
                function(msg) {
                    $.post("http://localhost:8080/user/upUser",
                        {
                            "tableuserId":tableuserId
                        }, function (data) {
                            $("#userName").val(data.userName);
                            tableuserName=data.userName;
                            $("#roleName").val(data.roleId.roleId);
                           // roleid=data.roleId.roleId;
                            $("#empName").val(data.hrEmp.empName);
                    for(var i=0;i<msg.length;i++){
                        if (data.roleId.roleId==msg[i].roleId) {
                            var option=$("<option value="+msg[i].roleId+" selected='selected'>"+msg[i].roleName+"</option>");
                        }
                        role.append(option);
                        layui.use('form', function(){
                            var form = layui.form;
                            form.render("select");
                        });
                    }
                    for(var j=0;j<msg.length;j++){
                        if (data.roleId.roleId!=msg[j].roleId) {
                            var option=$("<option value="+msg[j].roleId+">"+msg[j].roleName+"</option>");
                        }
                        role.append(option);
                        layui.use('form', function(){
                            var form = layui.form;
                            form.render("select");
                        });
                    }
                    })
                })

            // var userName=$("#userName").val();
            // $.post("http://localhost:8080/user/insUserName",
            //     {"userName": userName}
            //     , function (msg) {
            //         if (msg == 200) {
            //             $("#fuserName").html("用户已存在，请重新输入用户名");
            //         } else {
            //
            //         }
            //     });

            //返回按钮
            $("#return").click(function () {
                window.location.href="userList.html";
            })
            $("#sub").click(function(){
                var userName=$("#userName").val();
                var roleName=$("#roleName").val();
                if(tableuserName!=userName){
                $.post("http://localhost:8080/user/insUserName",
                    {"userName": userName}
                    , function (msg) {
                        if (msg == 200) {
                            $("#fuserName").html("用户已存在，请重新输入用户名");
                        }
                    });
                }else {
                    $.post("http://localhost:8080/user/updateUser",{
                        "userName":userName,
                        "roleName":roleName,
                        "userid":tableuserId
                    },function() {
                        window.location.href="userList.html";
                    })
                }
            })
        })
    </script>


</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    用户名</label>
                <div class="layui-input-inline">
                    <input type="text" id="userName" name="userName" required="" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <font id="fuserName" style="color: red"></font>
            </div>
            <div class="layui-form-item">
                <label for="roleName" class="layui-form-label">
                    角色名称:</label>
                <div class="layui-input-inline">
                    <select id="roleName" name="roleName" class="valid">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="empName" class="layui-form-label">
                    员工名称:</label>
                <div class="layui-input-inline">
                    <input disabled id="empName" autocomplete="off" class="layui-input">
                </div>
            </div>
            <button class="layui-btn" id="sub" type="button">修改</button>
            <button class="layui-btn" type="button" class="layui-btn layui-btn-sm" id="return">取消</button>
        </form>
    </div>
</div>
</body>
</html>